#!/bin/bash
set -o pipefail;

#TODO:activate when ready
# remove these comments and the exit statement; for now fail it
#exit 1

PGNAME=`basename $0`
PGDIRNAME=`dirname $0`
PGERR="$PGNAME[$$]: "
PGUSER=`id -un`


if [ "${MLPROD_HOME}" == "" ]
then
  echo "${PGERR} :  MLPROD_HOME env not set;  exiting !!!" >&2
  exit 10
fi

PGCFG_FILE="${MLPROD_HOME}/config/mlprog.rc"
if [ ! -f ${PGCFG_FILE} ]
then
  echo "${PGERR} : config file <${PGCFG_FILE}> not found; exiting !!!" >&2
  exit 10
fi

. "${PGCFG_FILE}"

CUST_CTX="$(mlget_cust_ctx 'cust')"
MOD_CTX="$(mlget_cust_ctx  'mod')"
BATCH_WFL_ID=1                  # workflow/product id
BATCH_MODE="oper"
if [ ! -z ${BATCH_RUN_MODE} ]
then
  BATCH_MODE="${BATCH_RUN_MODE}"
fi
LOG_CTX="${BATCH_MODE}-${CUST_CTX}"


PGCFG_FILE="${MLPROD_HOME}/cust/${CUST_CTX}/config/${MOD_CTX}.rc"
if [ ! -f ${PGCFG_FILE} ]
then
  echo "${PGERR} : config file <${PGCFG_FILE}> not found; exiting !!!" >&2
  exit 10
fi

. "${PGCFG_FILE}"
if [ ! -f ${PGCFG_FILE} ]
then
  echo "${PGERR} : config file sourcing <${PGCFG_FILE}> error; exiting !!!" >&2
  exit 10
fi


function cleanup
{
  rm -f ${EDER_DSTATUS_FILE};
  rm -f ${EDER_ESTATUS_FILE};

  if [ -f ${OUT_INSURANCE_FILE} ]
  then
    rm -f ${OUT_INSURANCE_FILE}
    if [ $? -ne 0 ]
    then
      mllogerr "[${LOG_CTX}] : ERR : Unable to cleanup files : ${OUT_INSURANCE_FILE}";
    fi
  fi

}

trap cleanup EXIT ERR TERM INT QUIT

function usage()
{
  mllogerr "Usage: $PGNAME -p [0|1|200] -b batch_recv_date_in_MMDDYY_format";
}

if [ $# -ne 4 ]
then
  usage;
  exit 10;
fi


PART=-1
DBATE="";
while getopts "p:b:" opts
do
  case $opts in
    p ) PART=$OPTARG
        ;;
    b ) BDATE=$OPTARG
        ;;
    * ) usage; exit 10;
        ;;
  esac
done

if [ ${PART} -lt 0 -o ${PART} -gt 2 -a ${PART} -ne 200 -o -z ${BDATE} ]
then
  usage
  exit 10
fi


# get the batch dates BATCH_RECV_DATE, BATCH_RUN_DATE, BATCH_EXEC_DATE
mlget_batch_dates "${BDATE}"
if [ $? -ne 0 ] || [ -z "${BATCH_RECV_DATE}" ]
then
  usage
  exit 10
fi

# LOG
LOG_CTX="${LOG_CTX}-${BATCH_RECV_DATE}"

# EMDEON FILE
IN_INSURANCE_FILE=${ARCH_CTX_IN_DIR}/${BATCH_RECV_DATE}_emdeon.gpg


# RECON OUTPUT FILE
RECON_OUTPUT=${MLARCH_DIR}/recon/in


if [ ! -d ${LOG_CTX_DIR} ]
then
  mkdir -p ${LOG_CTX_DIR}
  if [ $? -ne 0 ]
  then
    mllogerr "[${LOG_CTX}] : ERR : Unable to create log folder : ${LOG_CTX_DIR}";
    exit 10;
  fi
fi

if [ ! -r ${IN_INSURANCE_FILE} ]
then
  mllogerr "[${LOG_CTX}] : ERR : Incoming Emdeon Insurance file not found : ${IN_INSURANCE_FILE}";
  exit 10;
fi

tstart=$((`date +%s`))

tstamp="00:00:00"
mllogerr "[${LOG_CTX}] : BEG : [${tstamp}] : BATCH START"

#---------------------------------
# If PART==200, skip this testing
#---------------------------------
if [ $PART -eq 0 -o $PART -eq 1 ]
then
    EDER_DSTATUS_FILE=${BATCH_CTX_DIR}/${BATCH_RECV_DATE}_emdeon.d
    DECR="${DECR} -s ${EDER_DSTATUS_FILE}"
    ${DECR} -f ${IN_INSURANCE_FILE} > ${ARCH_CTX_IN_DIR}/${BATCH_RECV_DATE}_emdeon
    ${PGDIRNAME}/../common/verify_emdeon_data.pl ${BATCH_RECV_DATE} ${ARCH_CTX_IN_DIR}/${BATCH_RECV_DATE}_emdeon
    RET=$?
    /bin/rm -f ${ARCH_CTX_IN_DIR}/${BATCH_RECV_DATE}_emdeon
    if [ $RET -ne 0 ]
    then
       mllogerr "[${LOG_CTX}] : ERR : verify_emdeon_data.pl failed"
       cleanup;
       exit 10;
    fi
fi

#----------------------------------------
# If PART==200, set PART=0.
# From now on, the execution of P#=200
# is exactly same as P#=0.
#----------------------------------------
if [ $PART -eq 200 ]
then
	PART=0
fi

if [ $PART -eq 0 -o $PART -eq 1 ]
then
  ${PGDIRNAME}/run_part_1.sh ${BATCH_RECV_DATE}
  RET=$?
  if [ $RET -ne 0 ]
  then
    mllogerr "[${LOG_CTX}] : ERR : run batch part-1 failed"
    cleanup;
    exit $RET;
  fi

fi



if [ $PART -eq 0 ] && [ $MMACHINE -eq 1 ]
then

	TASK_ID=`expr $TASK_ID + 1`
	tmsg="output file into /archive/recon/in folder";
	mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
	ts=$((`date +%s`))
	${PGSQL} ${SQL_CTX_BAK_DIR}/copy_table_insurance2_output_recon.sql > "${RECON_OUTPUT}/${BATCH_RECV_DATE}.recon"
	if [ $? -ne 0 ]
	then
 	mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
  	cleanup;
  	exit -1;
	fi
	te=$((`date +%s`))


        FILE_COUNT=`wc -l "${RECON_OUTPUT}/${BATCH_RECV_DATE}.recon" | grep -i '^0 ' `

        if [ -z "$FILE_COUNT" ]
        then

           TASK_ID=`expr $TASK_ID + 1`
           tmsg="create trigger recon file in /haris/batch/payorintel/recon folder";
           mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
           ts=$((`date +%s`))
           touch "${MLPROD_HOME}/batch/payorintel/recon/${BATCH_RECV_DATE}.0"
           if [ $? -ne 0 ]
           then
             mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
             cleanup;
             exit -1;
           fi
           te=$((`date +%s`))

        else

           rm -f "${RECON_OUTPUT}/${BATCH_RECV_DATE}.recon"

        fi


	${PGDIRNAME}/../common/payorintel_report.pl ${BATCH_RECV_DATE} \
    	"${FTP_CTX_DIR}/auto/outgoing" ${ARCH_CTX_OUT_DIR}
	if [ $? -ne 0 ]
	then
  		mllogerr "[${LOG_CTX}] : ERR : Batch insurance2 reporting failed"
  		cleanup;
  		exit -1;
	fi


        TASK_ID=`expr $TASK_ID + 1`
        tmsg="output a summary table for management";
        mllogerr "[${LOG_CTX}] : INF : TASK-${TASK_ID} $tmsg";
        ts=$((`date +%s`))
        ${PGDIRNAME}/../common/report_management_summary.py -b ${BATCH_RECV_DATE}
        if [ $? -ne 0 ]
        then
           mllogerr "[${LOG_CTX}] : ERR : TASK-${TASK_ID} failed to ${tmsg}";
           cleanup;
           exit -1;
        fi
        te=$((`date +%s`))


fi


tstamp="$(mltstamp ${tstart})"
mllogerr "[${LOG_CTX}] : END : [${tstamp}] : BATCH DONE"

exit 0;
